package cn.szzsi._web;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.*;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 前台类 - 首页
 *
 */
public class IndexController extends BaseWebController<Member> {

    private List<ProductCategory> rootProductCategoryList;
    private List<Product> bestProductList;
    private List<Product> hotProductList;
    private List<Product> newProductList;
    private List<ProductCategory> allProductCategoryList;

    // 首页
    @Clear
	public void index(){
        String uid = getPara("uid");
        setAttr("uid",uid);

        //需要改造
        rootProductCategoryList = ProductCategory.dao.getBusinessRootProductCategoryList(uid);
        bestProductList = Product.dao.getBusinessBestProductList(uid,Product.MAX_BEST_PRODUCT_LIST_COUNT);
        hotProductList = Product.dao.getBusinessHotProductList(uid,Product.MAX_HOT_PRODUCT_LIST_COUNT);
        newProductList = Product.dao.getBusinessNewProductList(uid,Product.MAX_NEW_PRODUCT_LIST_COUNT);
        allProductCategoryList = ProductCategory.dao.getByUid(uid);
        Map<String, List<ProductCategory>> productCategoryMap = new HashMap<String, List<ProductCategory>>();
        Map<String, List<Product>> bestProductMap = new HashMap<String, List<Product>>();
        Map<String, List<Product>> hotProductMap = new HashMap<String, List<Product>>();
        Map<String, List<Product>> newProductMap = new HashMap<String, List<Product>>();
        for (ProductCategory productCategory : allProductCategoryList) {
            String productCategory_id = productCategory.getStr("id");
            productCategoryMap.put(productCategory_id, productCategory.getBusinessChildrenProductCategoryList(uid,productCategory));
            bestProductMap.put(productCategory_id, Product.dao.getBusinessBestProductList(uid,productCategory_id, Product.MAX_BEST_PRODUCT_LIST_COUNT));
            hotProductMap.put(productCategory_id, Product.dao.getBusinessHotProductList(uid,productCategory_id, Product.MAX_HOT_PRODUCT_LIST_COUNT));
            newProductMap.put(productCategory_id, Product.dao.getBusinessNewProductList(uid,productCategory_id, Product.MAX_NEW_PRODUCT_LIST_COUNT));
        }
        setAttr("productCategoryMap", productCategoryMap);
        setAttr("bestProductMap", bestProductMap);
        setAttr("bestProductList", bestProductList);
        setAttr("hotProductMap", hotProductMap);
        setAttr("hotProductList", hotProductList);
        setAttr("newProductMap", newProductMap);
        setAttr("newProductList", newProductList);
        setAttr("rootProductCategoryList", rootProductCategoryList);

        setAttr("middleNavigationList", Navigation.dao.getBusinessMiddleNavigationList(uid));
        setAttr("bottomNavigationList", Navigation.dao.getBusinessBottomNavigationList(uid));

        setAttr("rootArticleCategoryList", ArticleCategory.dao.getBusinessRootArticleCategoryList(uid));
        setAttr("recommendArticleList", Article.dao.getBusinessRecommendArticleList(uid, Article.MAX_RECOMMEND_ARTICLE_LIST_COUNT));
        setAttr("hotArticleList", Article.dao.getBusinessHotArticleList(uid, Article.MAX_HOT_ARTICLE_LIST_COUNT));
        setAttr("newArticleList", Article.dao.getBusinessNewArticleList(uid, Article.MAX_NEW_ARTICLE_LIST_COUNT));
        List<ArticleCategory> allArticleCategory = ArticleCategory.dao.getByUid(uid);
        setAttr("allArticleCategoryList", allArticleCategory);
        Map<String, List<ArticleCategory>> articleCategoryMap = new HashMap<String, List<ArticleCategory>>();
        Map<String, List<Article>> recommendArticleMap = new HashMap<String, List<Article>>();
        Map<String, List<Article>> hotArticleMap = new HashMap<String, List<Article>>();
        Map<String, List<Article>> newArticleMap = new HashMap<String, List<Article>>();
        for (ArticleCategory articleCategory : allArticleCategory) {
            articleCategoryMap.put(articleCategory.getStr("id"), articleCategory.getBusinessChildrenArticleCategoryList(uid,articleCategory));
            recommendArticleMap.put(articleCategory.getStr("id"), Article.dao.getBusinessRecommendArticleList(uid,articleCategory, Article.MAX_RECOMMEND_ARTICLE_LIST_COUNT));
            hotArticleMap.put(articleCategory.getStr("id"), Article.dao.getBusinessHotArticleList(uid,articleCategory, Article.MAX_HOT_ARTICLE_LIST_COUNT));
            newArticleMap.put(articleCategory.getStr("id"), Article.dao.getBusinessNewArticleList(uid,articleCategory, Article.MAX_NEW_ARTICLE_LIST_COUNT));
        }
        setAttr("articleCategoryMap", articleCategoryMap);
        setAttr("recommendArticleMap", recommendArticleMap);
        setAttr("hotArticleMap", hotArticleMap);
        setAttr("newArticleMap", newArticleMap);

		render("/business/"+getAttr(CommonUtil.BUSINESS_TEMPLATE_PATH)+"/index.html");
	}
	/**
	 * 验证码
	 */
	@Clear
	public void captcha() {
//		int width = 0, height = 0, minnum = 0, maxnum = 0, fontsize = 0;
//		CaptchaRender captcha = new CaptchaRender();
//		if (isParaExists("width")) {
//			width = getParaToInt("width");
//		}
//		if (isParaExists("height")) {
//			height = getParaToInt("height");
//		}
//		if (width > 0 && height > 0)
//			captcha.setImgSize(width, height);
//		if (isParaExists("minnum")) {
//			minnum = getParaToInt("minnum");
//		}
//		if (isParaExists("maxnum")) {
//			maxnum = getParaToInt("maxnum");
//		}
//		if (minnum > 0 && maxnum > 0)
//			captcha.setFontNum(minnum, maxnum);
//		if (isParaExists("fontsize")) {
//			fontsize = getParaToInt("fontsize");
//		}
//		if (fontsize > 0)
//			captcha.setFontSize(fontsize, fontsize);
//		// 干扰线数量 默认0
//		captcha.setLineNum(1);
//		// 噪点数量 默认50
//		captcha.setArtifactNum(10);
//		// 使用字符 去掉0和o 避免难以确认
//		captcha.setCode("123456789");
//		 //验证码在session里的名字 默认 captcha,创建时间为：名字_time
//		// captcha.setCaptchaName("captcha");
//	    //验证码颜色 默认黑色
//		// captcha.setDrawColor(new Color(255,0,0));
//	    //背景干扰物颜色  默认灰
//		// captcha.setDrawBgColor(new Color(0,0,0));
//	    //背景色+透明度 前三位数字是rgb色，第四个数字是透明度  默认透明
//		// captcha.setBgColor(new Color(225, 225, 0, 100));
//	    //滤镜特效 默认随机特效 //曲面Curves //大理石纹Marble //弯折Double //颤动Wobble //扩散Diffuse
//		captcha.setFilter(CaptchaRender.FilterFactory.Curves);
//		// 随机色 默认黑验证码 灰背景元素
//		captcha.setRandomColor(true);
//		render(captcha);
	}
}
